import Buff from '@/Buff';
import BuffUtil from '@/BuffUtil';
import UnitStateUtil from '@/UnitStateUtil';
import BuffCommon from 'code/Core/Common/BuffCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';

/**
 * 冰冷武器
 */
export default class DK_ColdWeapon extends AbilityBase {
    static instance: DK_ColdWeapon;
    buffType: AppBuffType = {
        id: 'buff_DK_ColdWeapon',
        art: this.art,
        name: this.abilityName,
        ubertip: '受到的冰霜伤害增强',
        dur: 5,
        stackCountMax: 3,
        targetAttach: 'chest',
        targetArt: 'Abilities\\Spells\\NightElf\\Barkskin\\BarkSkinTarget.mdl',
        attribute: {},
    };
    shieldBuffType: AppBuffType = {
        id: 'buff_DK_ColdWeapon_shield',
        art: this.art,
        name: this.abilityName,
        ubertip: '冰霜之力为你提供了护盾',
        dur: 15,
        targetAttach: 'origin',
        targetArt: '冰霜甲\\sem_bing_shuang_jia.mdx',
        onDestroy: this.onBuffDestory.bind(this),
    };
    onBuffDestory(buff: Buff) {
        ShieldSystem.移除指定护盾(buff.getParent(), this.shieldBuffType.id, buff.getCaster());
    }
    constructor(id: string = 'Ablw') {
        super(id);
        DK_ColdWeapon.instance = this;
        this.init();
        EventManager.onTakeAttack(this.attackEffect);
    }
    cast(tarX: number, tarY: number, source: unit, target: unit, data?: any): void {
        let d = data as DamageEventData;
        let _this = DK_ColdWeapon.instance;
        let lv = _this.getAbilityLevel(source);
        let val = -0.03 + -0.02 * lv;
        BuffCommon.addBuff({ target, caster: source, ablityId: _this.id, buffTypeId: _this.buffType.id, attribute: { 冰系伤害减免百分比: val } });
        let limit = UnitStateUtil.getMaxLife(source) * (0.06 + 0.04 * lv);
        let oldShield = ShieldSystem.获取按指定技能护盾值(source, ShieldSystem.getKey(_this.shieldBuffType.id, source));
        if (oldShield < limit) {
            let value = (d.lastDamage * (9 + lv)) / 100;
            value = oldShield + value > limit ? limit - oldShield : value;
            ShieldSystem.添加护盾值(source, _this.shieldBuffType.id, value, true, source);
        }
        BuffCommon.addBuff({ target: source, caster: source, ablityId: _this.id, buffTypeId: _this.shieldBuffType.id });
    }
    attackEffect(d: DamageEventData) {
        let _this = DK_ColdWeapon.instance;
        let lv = _this.getAbilityLevel(d.source);
        if (lv == 0) return;
        _this.cast(0, 0, d.source, d.target, d);
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
        BuffUtil.registerBuffType(this.shieldBuffType);
    }
}
